from PIL import Image
import os

def convert_to_jpg(folder_path):
    for filename in os.listdir(folder_path):
        filepath = os.path.join(folder_path, filename)
        if os.path.isfile(filepath):
            if filename.lower().endswith(('.png', '.jpeg', '.gif', '.bmp')):
                try:
                    with Image.open(filepath) as img:
                        jpg_filepath = os.path.splitext(filepath)[0] + '.jpg'
                        img.convert('RGB').save(jpg_filepath, 'JPEG')
                        print(f"Converted {filename} to JPG format.")
                        os.remove(filepath)
                except Exception as e:
                    print(f"Failed to convert {filename}: {str(e)}")

# 调用示例
folder_path = r'E:\temp\voc\JPEGImages'
convert_to_jpg(folder_path)
print("完成")